$(function() {
	var comment_id;
	var comment_name;
	var content_detail;
	var fid;
	var uid;
	var image1 = "";
	var image2 = "";
	var image3 = "";
	var loading = $("#my-modal-loading");
	var loadingtext = $("#loading_text");
	var requestModel = $("#my-actions");
	var btn = $("#bottom_more");
	var query_id = localStorage.getItem("query_id");
	var goods_id = localStorage.getItem("goods_id");
	var cid = localStorage.getItem("cid");
	var getGoodStatus = "http://yx.fxtourong.com/V1/Goods/getGoodsLaudStatus" + "?store_id=" + query_id;
	var goodStatus = 0;
	var goods = function(){};
	var urlPath = "http://yx.fxtourong.com/V1/Goods/getGoodsDetails" + "?store_id=" + query_id + "&goods_id=" + goods_id + "&cid=" + cid;
	var getCpmmentListUrl = "http://yx.fxtourong.com/V1/Goods/getGoodsComments" + "?store_id=" + query_id;
	var sendCpmmentListUrl = "http://yx.fxtourong.com/V1/Goods/commentsToGoods" + "?store_id=" + query_id;
	var huifuCpmmentListUrl = "http://yx.fxtourong.com/V1/Goods/replyToComment" + "?store_id=" + query_id;
	function shareBtn() {
		var url = window.location.href;
		var index = 1;
		$.ajax({
			type: "POST",
			url: "http://yx.fxtourong.com/V1/Wechat/getSignPackage",
			data: {
				token: localStorage.getItem("token"),
				url: url,
				verify: "yin@xiong8_(fx-tourong"
			},
			dataType: "json",
			success: function(data) {
				loading.modal('close');
				var code = data.code;
				if(code == "1") {
					console.log('data', data);
					appId = data.data.appId;
					timestamp = data.data.timestamp;
					nonceStr = data.data.nonceStr;
					signature = data.data.signature;
					wx.config({
						debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
						appId: appId, // 必填，公众号的唯一标识
						timestamp: timestamp, // 必填，生成签名的时间戳
						nonceStr: nonceStr, // 必填，生成签名的随机串
						signature: signature, // 必填，签名，见附录1
						jsApiList: ['onMenuShareAppMessage', 'onMenuShareTimeline', 'onMenuShareQQ'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
					});
					wx.ready(function() {
						wx.onMenuShareAppMessage({
							title: '分享动态', // 分享标题
							desc: content_detail, // 分享描述
							link: share_url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
							imgUrl: '', // 分享图标
							type: 'link', // 分享类型,music、video或link，不填默认为link
							dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
							success: function() {
								// 用户确认分享后执行的回调函数
							},
							cancel: function() {
								// 用户取消分享后执行的回调函数
							}
						});
					});
				} else {
					alert("初始化失败:" + data.message);
				}
			},
			error: function(e) {
				loading.modal('close');
				alert("服务请求异常！");
			}
		});
	}
	
	function getData(){
		$.ajax({
			type: "GET",
			url: urlPath,
			data: {
			},
			dataType: "json",
			success: function(data) {
				var code = data.code;
				console.log(code);
				if(code == "1") {
					$("#dongtai_head").attr("src", data.data.avatar);
					$("#dongtai_username").text(data.data.nickname);
					$("#dongtai_time").text(data.data.created_time);
					$("#topicDetails").text(data.data.details);
					content_detail = data.data.details;
					var g_type = data.data.g_type;
					console.log("g_type:" + g_type);
					if(g_type == "2"){
						var countimage = data.data.cover.length;
						for(var i = 0; i < countimage; i++){
							if(i == 0){
								image1 = data.data.cover[i];
							} else if(i == 1){
								image2 = data.data.cover[i];
							} else {
								image3 = data.data.cover[i];
							}
						}
						if(countimage == 1){
							$("#image1").attr('src', image1);
							$("#image1").css("display", "inline");
						} else if (countimage == 2){
							$("#image1").attr('src', image1);
							$("#image1").css("display", "inline");
							$("#image2").attr('src', image2);
							$("#image2").css("display", "inline");
						} else {
							$("#image1").attr('src', image1);
							$("#image1").css("display", "inline");
							$("#image2").attr('src', image2);
							$("#image2").css("display", "inline");
							$("#image3").attr('src', image3);
							$("#image3").css("display", "inline");
							
						}
					} else {
						$('.swiper-container').css("display", "none");
					}
					var goodstype = new goods();
					goodstype.getGoodStatus();
				} else {
					loading.modal('close');
					alert(data.message + "");
				}
			},
			error: function(e){
				loading.modal('close');
				alert("服务请求异常！");
			}
		});	
	}
	
		
	var page = 1;
	var jsonStr;
	var person_comment;
	$("#sendMsg").click(function() {
		person_comment = $("#person_comment").text();
		console.log("评论:" + person_comment);
		loadingtext.text("发送评论...");
		loading.modal();
		$.ajax({
			type: "POST",
			url: sendCpmmentListUrl,	//44
			data: {
				token : localStorage.getItem("token"),
				goods_id : goods_id,
				content : person_comment
			},
			dataType: "json",
			success: function(data) {
				loading.modal('close');
				var code = data.code;
				console.log(code);
				if(code == "1") {
					location.href = "dongtai_detail.html";
				} else {
					alert(data.message + "");
				}
			},
			error: function(e){
				loading.modal('close');
				alert("服务请求异常!");
			}
		});
	});
	
	btn.click(function(){
		loading.modal();
		page = page + 1;
		getCommentList();
	});
	
	$("#image_slide").click(function(){
		localStorage.setItem("yulan_image1", image1);
		localStorage.setItem("yulan_image2", image2);
		localStorage.setItem("yulan_image3", image3);
		location.href = "preview_pics2.html";
	});
	
	var ary = new Array();
	
	function getCommentList(){
		$.ajax({
			type: "POST",
			url: getCpmmentListUrl,  //12  store_id
			data: {
				goods_id: goods_id,
				page: page
			},
			dataType: "json",
			success: function(data) {
				$(".comment_list").css("display", "block");
				var code = data.code;
				console.log("评论列表code:" + data.code);
				if(code == "1") {
					jsonStr = data.data;
					if(page == 1){
						if(jsonStr.length == 10){
							btn.css("display", "block");
						}
					}
					for(var i = 0; i<jsonStr.length; i++){
						var industry = jsonStr[i];	
						ary.push(industry);
						var timeStr = industry.created_time;
						var commentDetails = industry.content;
						var fid = industry.fid;
						if(fid == "0"){
							comment_name = industry.nickname;
							$("#comment_Ul").append("<li value='" + (i + 10*(page - 1)) + "' class='topiclist' style='border-bottom:none;border-top:0px solid #DEDEDE;padding-top:0px;margin-top:0px;margin-bottom:0px;' onclick=''>" + 
							"<div class='usershead'> "+ 
								"<img id='imagehead2' class='am-circle' src='" + industry.avatar + "' width='40' height='40' style='margin: 0 10px 0 0;'/>" + 
							"</div>" +
							"<div class='userMessage'>" + 
								"<p class='usersname'>" + industry.nickname + "</p>" + 
								"<p class='thetime'>" + timeStr + "</p>" + 
							"</div>" + 
							"<p class='commentDetails' style='padding: 0 20px 20px 20px;margin: 10px auto 0 auto;'>" + commentDetails + "</p>" + 
							
						"</li>"
							
							);
						} else if(fid == "1") {
							
							$("#comment_Ul").append("<li value='" + (i + 10*(page - 1)) + "' class='topiclist' style='border-bottom:none;border-top:0px solid #DEDEDE;margin-left:30px;margin-top:-30px;' onclick=''>" + 
							
							"<p class='commentDetails' style='padding: 0 20px 20px 20px;margin: 10px auto 0 20px;'><span style='color:#009CDA'>" + industry.from_nickname + "</span>" + " 回复 " + "<span style='color:#009CDA'>" + industry.to_nickname + "</span>" + " : " +  commentDetails + "</p>" + 
							
						"</li>"
							
							);
					} else {
						if(industry.from_nickname !== null && industry.from_nickname !== "" && industry.from_nickname !== undefined){
							$("#comment_Ul").append("<li value='" + (i + 10*(page - 1)) + "' class='topiclist' style='border-bottom:none;border-top:0px solid #DEDEDE;margin-left:30px;margin-top:-30px;' onclick=''>" + 
							
							"<p class='commentDetails' style='padding: 0 20px 20px 20px;margin: 10px auto 0 20px;'><span style='color:#009CDA'>" + industry.from_nickname + "</span>" + " 回复 " + "<span style='color:#009CDA'>" + industry.to_nickname + "</span>" + " : " +  commentDetails + "</p>" + 
							
						"</li>");
						}
						
					}
						
					}
					shareBtn();
				} else {
					loading.modal('close');
					btn.css("display", "none");
					if(page == 1){
						$(".comment_list").css("display", "none");
					}
//					alert(data.message + "");
				}
			},
			error:function(e){
				loading.modal('close');
				btn.css("display", "none");
				alert("服务请求异常!")
			}
		});
	}
	
	var huifu_detail;
	
	$("#huifu").click(function(){
		huifu_detail = $("#huifu_detail").val();
		console.log("回复评论:" + huifu_detail);
		loadingtext.text("发送回复...");
		loading.modal();
		var type = 0;
		if(fid == "0"){
			 type = 1;			
		} else {
			 type = 2;
		}
		$.ajax({
				type: "POST",
				url: huifuCpmmentListUrl,	//44
				data: {
					token : localStorage.getItem("token"),
					type: type,
					comment_id : comment_id,
					to_uid : uid,
					content : huifu_detail
				},
				dataType: "json",
				success: function(data) {
					loading.modal('close');
					var code = data.code;
					console.log(code);
					if(code == "1") {
						location.href = "dongtai_detail.html";
					} else {
						alert(data.message + "");
					}
				},
				error: function(e){
					loading.modal('close');
					alert("服务请求异常!");
				}
			});
	});
	
	$(document).on("click",".topiclist",function(){
		var m = $(this).val();
		fid = ary[m].fid;
		requestModel.modal();
		if(fid == "0"){
			uid = ary[m].uid;
			comment_name = ary[m].nickname;
			comment_id = ary[m].id;
			$("#reponse_person").text("回复: " + ary[m].nickname);
		} else {
			uid = ary[m].from_uid;
			comment_name = ary[m].from_nickname;
			comment_id = ary[m].comment_id;
			$("#reponse_person").text("回复: " + ary[m].from_nickname);
		}
		
	});
	
	var getGoodNum = "http://yx.fxtourong.com/V1/Goods/laudToGoods" + "?store_id=" + query_id;
	$("#goodu").click(function(){	//点赞
		if(goodStatus == 1){
			new Toast({context:$('body'),message:'您已经点赞'}).show();
			return;
		}
		loading.modal();
		$.ajax({
				type: "POST",
				url: getGoodNum,  //46 
				data: {
					token : localStorage.getItem("token"),
					goods_id: goods_id				
				},
				dataType: "json",
				success: function(data) {
					loading.modal('close');
					console.log(data);
					var code = data.code;
					console.log(code);
					if(code == "1") {
						$("#goodu").attr("src", "../../public/yx/img/good2.svg");
//						location.href = "dongtai_detail.html";													
					} else {
						alert(data.message + "");
					}
				},
				error:function(e){
					loading.modal('close');
					alert("服务请求异常!")
				}
			});
	});
	
	goods.prototype.getGoodStatus=function(name){
    	$.ajax({
				type: "POST",
				url: getGoodStatus,  //46 
				data: {
					token : localStorage.getItem("token"),
					goods_id: goods_id				
				},
				dataType: "json",
				success: function(data) {
					loading.modal('close');
					console.log(data);
					var code = data.code;
					console.log(code);
					if(code == "1") {
						$("#goodu").attr("src", "../../public/yx/img/good2.svg");
						goodStatus = 1;
					} else {
						$("#goodu").attr("src", "../../public/yx/img/good.svg");
						goodStatus = 0;
					}
					getCommentList();
				},
				error:function(e){
					loading.modal('close');
					alert("服务请求异常!")
				}
			});
	}
	loading.modal();
	getData();
});